package com.briup.web.servlet;

import com.briup.bean.Customer;
import com.briup.service.ICustomerService;
import com.briup.service.impl.ICustomerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 王若龙
 * @CreateTime 2022-09-22 11:10
 */
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {

    private ICustomerService iCustomerService = new ICustomerServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String zipCode = req.getParameter("zipCode");
        String address = req.getParameter("address");
        String telephone = req.getParameter("telephone");
        String email = req.getParameter("email");

        Customer customer = new Customer(name, password, zipCode, address, telephone, email);
        try {
            iCustomerService.register(customer);
            resp.sendRedirect("/login.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            req.getSession().setAttribute("error", e.getMessage());
            resp.sendRedirect("/register.jsp");
        }

    }
}
